<template>
    <div class="notification">
        <div class="bg">
            <div class="logo" @click="toNoticList">
                <img src="../../assets/logo.png" alt="" @click.stop="toHome">
                <div>
                    通知公告
                </div>
            </div>
        </div>
        <div class="search">
            <el-form ref="ruleForm" :model="queryParams" :inline="true" class="demo-form-inline" label-width="100px">
                <el-form-item label="关键字">
                    <el-input v-model="queryParams.title" clearable></el-input>
                </el-form-item>
                <el-form-item label="发布部门">
                    <el-select v-model="queryParams.department" clearable placeholder="请选择部门">
                        <el-option v-for="item in deptList" :key="item.value" :label="item.label" :value="item.label">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="开始时间">
                    <el-date-picker type="date" v-model="queryParams.startDate" value-format="yyyy-MM-dd">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="结束时间">
                    <el-date-picker type="date" v-model="queryParams.endDate" value-format="yyyy-MM-dd">
                    </el-date-picker>
                </el-form-item>
                <div class="search-btn">
                    <el-button type="primary" @click="resetForm('ruleForm')" icon="el-icon-refresh">重置</el-button>
                    <el-button @click="onSearch" type="primary" icon="el-icon-search">查询</el-button>
                    <!-- <el-button type="primary" icon="el-icon-user">我发布的</el-button> -->
                    <!-- <el-button type="primary" @click="noticAdd" icon="el-icon-circle-plus-outline">新建公告</el-button> -->
                </div>
            </el-form>
        </div>
        <div class="content">
            <div class="left">
                <h3>我发布的公告</h3>
                <ul>
                    <li :class="noticIndex == index ? 'active' : ''" @click="onChangeLi(index, item)"
                        v-for="(item, index) in noticClassify">{{ item }}</li>
                </ul>
            </div>
            <div class="right">
                <div class="head">
                    {{ noticTitle }}
                </div>
                <div class="list">
                    <div @click="toDetail(ele.id)" class="item" v-for="ele in noticeList">
                        <div class="left">
                            <span><img src="../../assets/new-list-style.png" alt="">{{ ele.title }}</span>
                            <span class="time">{{ ele.createdAt.slice(0, 10) }}</span>
                        </div>
                        <div class="right">
                            <el-button type="primary" @click.stop="onEditNotic(ele.id)" icon="el-icon-edit"
                                circle></el-button>
                            <el-button type="danger" @click.stop="onDelNotic(ele.id)" icon="el-icon-delete"
                                circle></el-button>
                        </div>
                    </div>
                </div>
                <div class="pagination">
                    <el-pagination :background="true" @size-change="handleSizeChange"
                        @current-change="handleCurrentChange" :current-page.sync="currentPage" :page-size="10"
                        layout="total, prev, pager, next" :total="50">
                    </el-pagination>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import { conditionQueryNotice, listNoticeTypes, departmentList, noticeDel } from "@/api/notice";
export default {
    name: 'NotificationAnnouncement',
    data() {
        return {
            noticIndex: 0,
            noticTitle: "全部通知",
            noticClassify: ["全部通知", "公告通知", "会议通知", "院内通知", "党字公文", "其他公文"],
            noticeList: [],
            currentPage: 1,
            deptList: [],//部门列表
            queryParams: {
                types: "",
                title: "",
                startDate: "",
                endDate: "",
                department: "",
                senderId: localStorage.getItem("userId")
            }
        }
    },
    mounted() {
        this.getNoticList(this.queryParams);
        listNoticeTypes().then(res => {
            console.log("通知类型列表", res.data);
            let obj_list = res.data;
            const types = obj_list.map(item => item.ntypeName);
            types.unshift("全部通知");
            this.noticClassify = types;
        })
        this.getDepartmentList();
    },
    methods: {
        //重置表单
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
        toNoticList() {
            this.$router.push("/notification")
        },
        toHome(){
            this.$router.push("/teacher")
        },
        //获取部门列表
        getDepartmentList() {
            departmentList({ size: 50 }).then(res => {
                // this.deptList=res.data;
                this.deptList = res.data.map(dept => {
                    return {
                        value: dept.id,
                        label: dept.name,
                    }
                })
                console.log("部门列表", this.deptList);
            })
        },
        //获取通知列表
        getNoticList(query) {
            conditionQueryNotice(query).then(res => {
                this.noticeList = res.data;
            })
        },
        onSearch() {
            this.getNoticList(this.queryParams);
        },
        onChangeLi(index, item) {
            this.noticIndex = index;
            this.noticTitle = item;
            if (item != "全部通知") {
                this.queryParams.types = item;
                this.getNoticList(this.queryParams);
            } else {
                this.queryParams.types = "";
                this.getNoticList(this.queryParams);
            }
        },
        handleSizeChange() {

        },
        handleCurrentChange(val) {
            console.log(`当前页: ${val}`);
        },
        toDetail(id) {
            this.$router.push("/noticDetail?id=" + id)
        },
        noticAdd() {
            this.$router.push("/noticAdd")
        },
        //编辑
        onEditNotic(id) {
            this.$router.push("/noticEdit?id=" + id)
        },
        //删除
        onDelNotic(id) {
            this.$confirm('确定删除吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                noticeDel(id).then(res => {
                    if (res.code == 200) {
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                        this.getNoticList(this.queryParams)
                    }
                })

            }).catch(() => { });
        }
    }
}
</script>

<style lang="scss" scoped>
.notification {
    min-width: 1200px;

    .bg {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        height: 110px;
        z-index: 999;
        background: url("../../assets/notic-bg.jpg") no-repeat;

        .logo {
            padding: 10px 40px;
            display: flex;
            align-items: center;

            &>div {
                color: white;
                margin-left: 10px;
                border-left: 1px solid white;
                padding: 14px 0;
                padding-left: 15px;
            }
        }
    }

    .banner {
        width: 1200px;
        height: 300px;
        margin-left: auto;
        margin-right: auto;
        margin-top: 120px;

        img {
            height: 100%;
            width: 100%;
        }
    }

    .search {
        width: 1200px;
        margin-left: auto;
        margin-right: auto;
        margin-top: 150px;
        display: flex;
        flex-wrap: wrap;
        border: 1px solid rgba(0, 0, 0, .1);
        padding: 20px;
        box-sizing: border-box;

        .el-form {
            display: flex;
            flex-wrap: wrap;

            .el-form-item {
                width: 50%;
                margin-right: 0;
                display: flex;

                :deep .el-form-item__content {
                    flex: 1;
                }

                .el-select,
                .el-date-editor {
                    width: 100%;
                }
            }
        }

        .search-btn {
            text-align: right;
            display: flex;
            width: 100%;
            justify-content: flex-end;
        }
    }

    .content {
        max-height: calc(100vh - 122px);
        width: 1200px;
        margin-left: auto;
        margin-right: auto;
        margin-top: 30px;
        margin-bottom: 30px;
        display: flex;

        &>.left {
            background-color: #0D67BB;
            width: 260px;
            padding: 20px 0 10px 20px;

            h3 {
                color: white;
                font-size: 24px;
                text-align: center;
            }

            ul {
                background-color: white;
                position: relative;
                z-index: 100;
                background: white;
                padding-bottom: 60px;
                color: #666;
                margin-top: 15px;
                border-right: 1px solid rgba(0, 0, 0, .1);

                li {
                    list-style: none;
                    padding: 20px 20px;
                    border-bottom: 1px solid rgba(0, 0, 0, .1);
                    text-align: center;

                    &:hover,
                    &.active {
                        color: #fff;
                        border: 0px solid rgba(0, 0, 0, 0);
                        background: rgba(13, 103, 187, 0.5);
                    }
                }
            }
        }

        &>.right {
            flex: 1;
            margin-left: 40px;

            .head {
                padding: 0 0 10px 0px;
                overflow: hidden;
                margin-bottom: 20px;
                box-sizing: border-box;
                border-bottom: 1px solid #0D67BB;
                color: #0D67BB;
                // line-height:20px;
                font-size: 20px;
                font-weight: bold;
            }

            .list {
                .item {
                    display: flex;
                    color: #333;
                    padding-bottom: 20px;
                    margin-bottom: 20px;
                    transition: all .3s ease-in-out;
                    border-bottom: 1px dashed rgba(0, 0, 0, .1);

                    .left {
                        flex: 1;
                        display: flex;
                        flex-direction: column;

                        span:first-child {
                            flex: 1;

                            img {
                                margin-right: 10px;
                                vertical-align: middle;
                            }
                        }

                        .time {
                            color: #999;
                            width: 100px;
                            margin-left: 20px;
                            margin-top: 15px;
                        }
                    }

                    .right {}


                }
            }
        }

        .pagination {
            margin-top: 10px;

            .el-pagination {
                text-align: right;
            }
        }
    }
}
</style>